package com.yuexin.project.dailymenu.dishlist.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuexin.common.constant.UserConstants;
import com.yuexin.common.support.Convert;
import com.yuexin.common.utils.StringUtils;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.project.dailymenu.dishlist.domain.DishList;
import com.yuexin.project.dailymenu.dishlist.mapper.DishListMapper;
import com.yuexin.project.dailymenu.dishtype.domain.DishType;
import com.yuexin.project.dailymenu.dishtype.service.ITypeService;
import com.yuexin.project.dailymenu.everyday.domain.EverydayMenu;
import com.yuexin.project.dailymenu.menumanager.domain.Manager;

/**
 * 菜品列 服务层实现
 * 
 * @author yuexin
 * @date 2018-10-10
 */
@Service
public class ListServiceImpl implements DishIListService 
{
	@Autowired
	private DishListMapper listMapper;
	
	@Autowired
	private ITypeService typeService;

	/**
     * 查询菜品列信息
     * 
     * @param dishId 菜品列ID
     * @return 菜品列信息
     */
    @Override
	public DishList selectListById(Integer dishId)
	{
	    return listMapper.selectListById(dishId);
	}
	
	/**
     * 查询菜品列列表
     * 
     * @param list 菜品列信息
     * @return 菜品列集合
     */
	@Override
	public List<DishList> selectListList(DishList list)
	{
		list.setCompanyId(ShiroUtils.getCompanyId());
	    return listMapper.selectListList(list);
	}
	
	/**
     * 根据菜品类别查询菜品列列表
     * 
     * @param list 菜品列信息
     * @return 菜品列集合
     */
	public List<DishList> selectListByDishTypeId(Integer typeId)
	{
	    return listMapper.selectListByDishTypeId(typeId);
	}
	
    /**
     * 新增菜品列
     * 
     * @param list 菜品列信息
     * @return 结果
     */
	@Override
	public int insertList(DishList list)
	{
		list.setCreateBy(ShiroUtils.getLoginName());
		list.setCompanyId(ShiroUtils.getCompanyId());
	    return listMapper.insertList(list);
	}
	
	/**
     * 修改菜品列
     * 
     * @param list 菜品列信息
     * @return 结果
     */
	@Override
	public int updateList(DishList list)
	{
		list.setUpdateBy(ShiroUtils.getLoginName());
	    return listMapper.updateList(list);
	}

	/**
     * 删除菜品列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteListByIds(String ids)
	{
		return listMapper.deleteListByIds(Convert.toStrArray(ids));
	}
	
	/**
     * 根据菜单ID查询菜品
     *
     * @param manager 菜单对象
     * @return 菜品列表
     */
    @Override
    public List<Map<String, Object>> menuDishTreeData(Manager manager)
    {
        Integer menuId = manager.getMenuId();
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        DishList list = new DishList();
        list.setCompanyId(ShiroUtils.getCompanyId());
        List<DishList> dishList = selectListList(list);
        
        if (StringUtils.isNotNull(menuId))
        {
        	manager.setCompanyId(ShiroUtils.getCompanyId());
            List<String> menuDishList = listMapper.selectMenuDishTree(manager);
            trees = getTrees(dishList, true, menuDishList);
        }
        else
        {
            trees = getTrees(dishList, false, null);
        }
        return trees;
    }
    
    /**
     * 对象转菜品树
     *
     * @param dishList 菜品列表
     * @param isCheck 是否需要选中
     * @param menuDishList 菜单已存在菜品列表
     * @return
     */
    public List<Map<String, Object>> getTrees(List<DishList> dishList, boolean isCheck, List<String> menuDishList)
    {

        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (DishList list : dishList)
        {
            if (UserConstants.DEPT_NORMAL.equals(list.getStatus()))
            {
                Map<String, Object> dishMap = new HashMap<String, Object>();
                //System.out.println("菜品类别---"+list.getTypeId()+"菜品ID---"+list.getDishId()+"菜品名---"+list.getDishName());
                dishMap.put("id", list.getDishId());
                dishMap.put("pId", list.getTypeId());
                dishMap.put("name", list.getDishName());
                dishMap.put("title", list.getDishName()); 
                if (isCheck)
                {
                	dishMap.put("checked", menuDishList.contains(list.getDishId() + list.getDishName()));
                }
                else
                {
                	dishMap.put("checked", false);
                }
                trees.add(dishMap);
            }
        }
        
        
        DishType dishType = new DishType();
        dishType.setCompanyId(ShiroUtils.getCompanyId());
        List<DishType> types = typeService.selectTypeList(dishType);
        for (DishType type : types)
        {
        	Map<String, Object> dishMap1 = new HashMap<String, Object>();
        	dishMap1.put("id", type.getTypeId());
            dishMap1.put("pId", "");
            dishMap1.put("name", type.getTypeName());
            dishMap1.put("title", type.getTypeName());
            
            boolean checked = false;
            dishMap1.put("checked", checked);
            trees.add(dishMap1);
        }
        return trees;
    }
    
    
    /**
     * 对象转菜品树-每日菜单
     *
     * @param dishList 菜品列表
     * @param isCheck 是否需要选中
     * @param menuDishList 菜单已存在菜品列表
     * @return
     */
    public List<Map<String, Object>> getDailyTrees(List<DishList> dishList, boolean isCheck, List<String> menuDishList)
    {

        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (DishList list : dishList)
        {
            if (UserConstants.DEPT_NORMAL.equals(list.getStatus()))
            {
                Map<String, Object> dishMap = new HashMap<String, Object>();
                dishMap.put("id", list.getDishId());
                dishMap.put("pId", list.getTypeId());
                dishMap.put("name", list.getDishName());
                dishMap.put("title", list.getDishName()); 
                if (isCheck)
                {
                	dishMap.put("checked", menuDishList.contains(list.getDishId() + list.getDishName()));
                }
                else
                {
                	dishMap.put("checked", false);
                }
                trees.add(dishMap);
            }
        }
        
        
        DishType dishType = new DishType();
        dishType.setCompanyId(ShiroUtils.getCompanyId());
        List<DishType> types = typeService.selectTypeList(dishType);
        for (DishType type : types)
        {
        	Map<String, Object> dishMap1 = new HashMap<String, Object>();
        	dishMap1.put("id", type.getTypeId());
            dishMap1.put("pId", "");
            dishMap1.put("name", type.getTypeName());
            dishMap1.put("title", type.getTypeName());
            
            boolean checked = false;
            dishMap1.put("checked", checked);
            trees.add(dishMap1);
        }
        return trees;
    }
    
    /**
     * 根据菜单ID查询菜品
     *
     * @param manager 菜单对象
     * @return 菜品列表
     */
    @Override
    public List<Map<String, Object>> dailyMenuDishTreeData(EverydayMenu everydayMenu)
    {
    	everydayMenu.setCompanyId(ShiroUtils.getCompanyId());
        Integer dailyId = everydayMenu.getDailyId();
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        DishList list =  new DishList();
        list.setCompanyId(ShiroUtils.getCompanyId());
        List<DishList> dishList = selectListList(list);
        
        if (StringUtils.isNotNull(dailyId))
        {
        	everydayMenu.setCompanyId(ShiroUtils.getCompanyId());
            List<String> menuDishList = listMapper.selectDailyMenuDishTree(everydayMenu);
            trees = getDailyTrees(dishList, true, menuDishList);
        }
        else
        {
            trees = getDailyTrees(dishList, false, null);
        }
        return trees;
    }
    
    /**
     * 查询菜单菜品列表
     *
     * @param 
     * @return 菜品列表
     */
    public List<String> selectMenuDishList(Manager manager) {
    	return listMapper.selectMenuDishList(manager);
	}
    
    /**
     * 查询每日菜单菜品列表
     *
     * @param 
     * @return 菜品列表
     */
    public List<String> selectDailyMenuDishList(EverydayMenu everydayMenu) {
    	return listMapper.selectDailyMenuDishList(everydayMenu);
	}
}
